package com.zkthink.ceres.pay.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeRefundApplyModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.zkthink.ceres.pay.service.PayService;
import com.zkthink.ceres.pay.wechat.v3.entity.request.combine.CloseOrderRequest;
import com.zkthink.ceres.pay.wechat.v3.entity.request.combine.CombineOrderRequest;
import com.zkthink.ceres.pay.wechat.v3.entity.request.combine.NoticeOrderRequest;
import com.zkthink.ceres.pay.wechat.v3.entity.request.combine.QueryOrderRequest;
import com.zkthink.ceres.pay.wechat.v3.entity.result.combine.CloseOrderResult;
import com.zkthink.ceres.pay.wechat.v3.entity.result.combine.CombineOrderResult;
import com.zkthink.ceres.pay.wechat.v3.entity.result.combine.NoticeOrderResult;
import com.zkthink.ceres.pay.wechat.v3.entity.result.combine.QueryOrderResult;
import com.zkthink.ceres.pay.wechat.v3.service.ecommerce.CombinePayService;
import com.zkthink.exception.BizException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 * 业务实现类
 * 支付通知结果
 * </p>
 *
 * @author vivi.huang
 * @date 2020-05-17
 */
@Slf4j
@Service
@AllArgsConstructor
public class PayServiceImpl implements PayService {

    private Environment environment;

    private WxPayService wxService;

    private CombinePayService weChatPayService;
    @Autowired
    AlipayTradeAppPayRequest alipayRequest;
    @Autowired
    AlipayTradeWapPayRequest alipayTradeWapPayRequest;
    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private AlipayTradeRefundRequest alipayRefundRequest;

    @Override
    public <T> T unifiedOrder(WxPayUnifiedOrderRequest request) {
        try {
            return wxService.createOrder(request);
        } catch (WxPayException e) {
            log.error("统一下单异常，单号:[{}]", request.getOutTradeNo(), e);
            throw new BizException("微信统一下单支付异常");
        }
    }
    @Override
    public <T> AlipayTradeAppPayResponse unifiedOrder(AlipayTradeAppPayModel model) {

        try {
            alipayRequest.setBizModel(model);
            return alipayClient.sdkExecute(alipayRequest);

        } catch (AlipayApiException e) {
            log.error("统一下单异常，单号:[{}]", model.getOutTradeNo(), e);
            throw new BizException("支付宝统一下单支付异常");
        }
    }
    @Override
    public <T> AlipayTradeWapPayResponse unifiedOrder(AlipayTradeWapPayModel model) {

        try {
            alipayTradeWapPayRequest.setBizModel(model);
            return alipayClient.pageExecute(alipayTradeWapPayRequest);
        } catch (AlipayApiException e) {
            log.error("统一下单异常，单号:[{}]", model.getOutTradeNo(), e);
            throw new BizException("支付宝统一下单支付异常");
        }
    }
    @Override
    public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) {
        try {
            return wxService.parseOrderNotifyResult(xmlData);
        } catch (WxPayException e) {
            log.error("支付结果通知异常，内容:[{}]", xmlData, e);
            throw new BizException("支付结果通知异常");
        }
    }

    @Override
    public WxPayRefundResult refund(WxPayRefundRequest request) {
        request.setNotifyUrl(environment.getProperty("ceres.wx.ma.refund-notify-url"));
        try {
            return wxService.refund(request);
        } catch (WxPayException e) {
            log.error("申请退款异常, 单号:[{}]", request.getOutTradeNo(), e);
            throw new BizException("申请退款异常");
        }
    }
    @Override
    public AlipayTradeRefundResponse refund(AlipayTradeRefundApplyModel request) {
        alipayRefundRequest.setBizModel(request);
        try {
            return alipayClient.execute(alipayRefundRequest);
        } catch (AlipayApiException e) {
            log.error("申请退款异常, 单号:[{}]", request.getOutTradeNo(), e);
            throw new BizException("申请退款异常");
        }

    }
    @Override
    public WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) {
        try {
            return wxService.refundQuery(request);
        } catch (WxPayException e) {
            log.error("查询退款异常,单号:[{}]" , request.getOutTradeNo() , e);
            throw new BizException("查询退款异常");
        }
    }

    @Override
    public WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) {
        try {
            return wxService.queryOrder(request);
        } catch (WxPayException e) {
            log.error("查询支付异常,单号:[{}]" , request.getOutTradeNo(), e);
            throw new BizException("查询支付异常");
        }
    }

    @Override
    public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) {
        try {
            return wxService.parseRefundNotifyResult(xmlData);
        } catch (WxPayException e) {
            log.error("查询退款异常,内容:[{}]", xmlData, e);
            throw new BizException("查询退款异常");
        }
    }

    @Override
    public WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) {
        try {
            return wxService.closeOrder(request);
        } catch (WxPayException e) {
            log.error("关闭支付异常, 单号:[{}]", request.getOutTradeNo(), e);
            throw new BizException("关闭支付异常");
        }
    }

    @Override
    public CombineOrderResult combineCreateOrder(CombineOrderRequest request) {
        return weChatPayService.createCombineOrder(request);
    }

    @Override
    public QueryOrderResult combineQueryOrder(QueryOrderRequest request) {
        return weChatPayService.queryCombineOrder(request);
    }

    @Override
    public CloseOrderResult combineCloseOrder(CloseOrderRequest request) {
        return weChatPayService.closeCombineOrder(request);
    }

    @Override
    public NoticeOrderResult combineCloseOrder(NoticeOrderRequest request) {
        return weChatPayService.noticeOrder(request);
    }
}
